package com.github.caijh.mall.wechat.controller;

import javax.inject.Inject;

import com.alibaba.fastjson.JSONObject;
import com.github.caijh.framework.data.redis.Redis;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.constants.WeChatConstants;
import com.github.caijh.mall.wechat.service.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/admin/wechat/menu")
@Api(tags = "微信开放平台 -- 菜单管理")
public class WeChatController {

    @Autowired
    private WeChatService weChatService;

    @Inject
    private Redis redis;

    /**
     * 获取微信菜单
     *
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "获取自定义菜单")
    @GetMapping(value = "/public/get")
    @ApiImplicitParam(name = "isAsync", value = "是否同步微信数据, true是，false否")
    public CommonResult<Object> get(@RequestParam(name = "isAsync", defaultValue = "false") Boolean isAsync) {
        JSONObject list = redis.get(WeChatConstants.REDIS_PUBLIC_MENU_KEY);
        if (list == null || isAsync) {
            //如果没有， 去读取
            list = weChatService.get();
            redis.set(WeChatConstants.REDIS_PUBLIC_MENU_KEY, list);
        }
        return CommonResult.success(list);
    }

    /**
     * 创建微信菜单
     *
     * @param data 菜单数据，具体json格式参考微信开放平台
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "保存自定义菜单")
    @PostMapping(value = "/public/create")
    public CommonResult<JSONObject> create(@RequestBody String data) {
        return CommonResult.success(weChatService.create(data));
    }

    /**
     * 删除微信菜单
     *
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "删除自定义菜单")
    @GetMapping(value = "/public/delete")
    public CommonResult<JSONObject> delete() {
        return CommonResult.success(weChatService.delete());
    }

    //自定义菜单

    /**
     * 获取微信菜单
     *
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "获取个性化菜单")
    @GetMapping(value = "/public/self/get")
    @ApiImplicitParam(name = "isAsync", value = "是否同步微信数据, true是，false否")
    public CommonResult<Object> getSelf(@RequestParam(name = "isAsync", defaultValue = "false") Boolean isAsync) {
        JSONObject list = redis.get(WeChatConstants.REDIS_PUBLIC_MENU_SELF_KEY);
        if (list == null || isAsync) {
            //如果没有， 去读取
            list = weChatService.getSelf();
            redis.set(WeChatConstants.REDIS_PUBLIC_MENU_SELF_KEY, list);
        }
        return CommonResult.success(list);
    }

    /**
     * 创建微信菜单
     *
     * @param data 菜单数据，具体json格式参考微信开放平台
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "保存个性化菜单")
    @PostMapping(value = "/public/self/create")
    public CommonResult<JSONObject> createSelf(@RequestBody String data) {
        return CommonResult.success(weChatService.createSelf(data));
    }

    /**
     * 删除微信菜单
     *
     * @param menuId 菜单id
     * @author Mr.Zhang
     * @since 2020-04-22
     */
    @ApiOperation(value = "删除个性化菜单")
    @GetMapping(value = "/public/self/delete")

    //参数说明
    @ApiImplicitParam(name = "menuId", value = "菜单ID")
    public CommonResult<JSONObject> deleteSelf(@RequestParam(name = "menuId") String menuId) {
        return CommonResult.success(weChatService.deleteSelf(menuId));
    }

}



